//
//  HWVersionBouncedView.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/9/7.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWVersionBouncedView.h"
#import <WebKit/WebKit.h>

@interface HWVersionBouncedView()<WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler> {
    
    
    WKWebView *_webView;
}

@property(strong,nonatomic) UIView *grayView;
//@property(strong,nonatomic) UIView *whiteView;
@property(strong,nonatomic) UIView *fenxiangView;
@property(strong,nonatomic) UILabel *miahsulabel;
@property(strong,nonatomic) UIImageView *icoimageView;


@property(strong,nonatomic) UIView *webviewwangyeView;

@property(strong,nonatomic) UIButton *quxiaoBut;
@property(strong,nonatomic) UIButton *shengjingBut;
@property(strong,nonatomic) UILabel *leironglabel;
 







@end



@implementation HWVersionBouncedView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        // 添加灰色背景
            [self addgrayView];
            
            [self setupUI];
            
        
    }

    
    return  self;
     
}



//  设置UI
- (void)setupUI {
    
     self.fenxiangView = [[UIView alloc]initWithFrame:CGRectMake(40, (kScreenH-400)/2, kScreenW -80, 450)];
     self.fenxiangView.backgroundColor =  [UIColor whiteColor];
     self.fenxiangView.layer.cornerRadius = 14;
    self.fenxiangView.layer.masksToBounds = YES;
    [self addSubview:self.fenxiangView];
    
    
 
    
    
    
    self.icoimageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW -80, 125)];
    self.icoimageView.image = [UIImage imageNamed:@"chahuatupian"];
    [self.fenxiangView addSubview:self.icoimageView];
    
    
    
    
          UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,  self.icoimageView.bottom, kScreenW -80, 25)];
           //  label.tag = 10088;
          //   label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
             label.font = [UIFont systemFontOfSize:17];
            label.textColor = QCmorenzitiHeiSe;
    
            // label.shadowColor = [UIColor colorWithWhite:.9f alpha:1.0f];
            //label.shadowOffset = CGSizeMake(0.0f, 1.0f);
            label.backgroundColor = [UIColor clearColor];
            //        label.textAlignment = UITextAlignmentLeft;
            label.text = @"App发现新版本";
           label.textAlignment =  NSTextAlignmentCenter;

           [self.fenxiangView addSubview:label];
    
    
    
     self.webviewwangyeView = [[UIView alloc]initWithFrame:CGRectMake(0, label.bottom , kScreenW -80, 240)];
     self.webviewwangyeView.backgroundColor = [UIColor whiteColor];
     [self.fenxiangView addSubview:self.webviewwangyeView];
    
    
    
    
        UIButton *weixingBut = [UIButton buttonWithType:UIButtonTypeCustom];
        weixingBut.frame = CGRectMake(20,self.webviewwangyeView.bottom+8, 70, 44);
       // [weixingBut setTitle:@"忘记密码？" forState:UIControlStateNormal];
      //  [weixingBut setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
        weixingBut.titleLabel.font = [UIFont systemFontOfSize:14];
        [weixingBut addTarget:self action:@selector(cancelButtap) forControlEvents:UIControlEventTouchUpInside];
    
       // wangjibutton.backgroundColor = [UIColor redColor];
       //  self.weixingBut.backgroundColor = [UIColor redColor];
        weixingBut.layer.cornerRadius = 10;
        weixingBut.layer.masksToBounds = YES;
        // [weixingBut setBackgroundImage:[UIImage imageNamed:@"weixin"] forState:UIControlStateNormal];
        [weixingBut setTitle:@"取消" forState:UIControlStateNormal];
        weixingBut.layer.cornerRadius = 10;
        weixingBut.layer.masksToBounds = YES;
    self.quxiaoBut = weixingBut;
    
        [weixingBut  setTitleColor:QCmorenzitiHeiSe forState:UIControlStateNormal];
        weixingBut.backgroundColor = RGB(247, 247, 247);
        [self.fenxiangView   addSubview:weixingBut];
    
    
           UIButton *weixingBut1 = [UIButton buttonWithType:UIButtonTypeCustom];
             weixingBut1.frame = CGRectMake(20+70+10,self.webviewwangyeView.bottom+8, kScreenW -80-20-70-10-20, 44);
            // [weixingBut setTitle:@"忘记密码？" forState:UIControlStateNormal];
           //  [weixingBut setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
             weixingBut1.titleLabel.font = [UIFont systemFontOfSize:14];
             [weixingBut1 addTarget:self action:@selector(upgradeButtap) forControlEvents:UIControlEventTouchUpInside];
         
            // wangjibutton.backgroundColor = [UIColor redColor];
            //  self.weixingBut.backgroundColor = [UIColor redColor];
             weixingBut1.layer.cornerRadius = 10;
             weixingBut1.layer.masksToBounds = YES;
             // [weixingBut setBackgroundImage:[UIImage imageNamed:@"weixin"] forState:UIControlStateNormal];
             [weixingBut1 setTitle:@"升级" forState:UIControlStateNormal];
             weixingBut1.layer.cornerRadius = 10;
             weixingBut1.layer.masksToBounds = YES;
         
             [weixingBut1  setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
             weixingBut1.backgroundColor = RGB(247, 247, 247);
             weixingBut1.backgroundColor =  [UIColor  bm_colorGradientChangeWithSizeView:weixingBut1];
            self.shengjingBut = weixingBut1;
    
             [self.fenxiangView   addSubview:weixingBut1];
          
     
    
    
  
    
    
    
    
    
}



//
- (void)cancelButtap {
    
    
    [self removeFromSuperview];
    
    
    
    
}


- (void)upgradeButtap {
    
    
    NSString * fanwenStr = self.dataDic[@"apk_file_url"];
    
  
      fanwenStr = [fanwenStr  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     NSURL *url1 = [NSURL URLWithString:fanwenStr];
    [[UIApplication  sharedApplication] openURL:url1 options:nil completionHandler:^(BOOL success) {



     }];
    
    
  
    
    
}

 
- (void)refreshData {
    
    
    
    NSString *str1 = self.dataDic[@"update_log"];
    
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,0, kScreenW-80-10, 240)];
               //     nameLabel.text = fanlilistArr[i];
        //  nameLabel.backgroundColor =  RGB(248, 119, 6);
         nameLabel.backgroundColor = [UIColor whiteColor];
          nameLabel.textAlignment = NSTextAlignmentLeft;
          nameLabel.font = [UIFont systemFontOfSize:15];
          nameLabel.numberOfLines = 0;
    
          nameLabel.textColor = RGB(68, 68, 68);
     //     nameLabel.layer.cornerRadius = 8;
     //     nameLabel.layer.masksToBounds = YES;
          nameLabel.text = str1;
        [self.webviewwangyeView addSubview:nameLabel];
    
    
    
    
    
    
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
       configuration.userContentController = [[WKUserContentController alloc] init];
      // [configuration.userContentController addScriptMessageHandler:self name:@"gameinfo"];
       
     NSString *str = self.dataDic[@"update_log"];
    
     _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenW-80,240) configuration:configuration];
     _webView.UIDelegate = self;
     _webView.navigationDelegate = self;
    _webView.hidden = YES;
    
     _webView.scrollView.scrollEnabled = YES;
    
    
  NSString*jScript =@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
    WKUserScript *www =  [[WKUserScript alloc]initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    
    WKUserContentController *uController = [[WKUserContentController alloc]init];
    [uController addUserScript:www];
    configuration.userContentController = uController;
    
    
 

    
    
     [_webView loadHTMLString:str baseURL:nil];
     
     [self.webviewwangyeView addSubview:_webView];
    
    NSString *update = [NSString stringWithFormat:@"%@",self.dataDic[@"update"]];
    
    NSLog(@"%@",self.dataDic[@"update"]);
    
    if ([self.dataDic[@"constraint"] intValue] == 1) {
        
       
       // self.quxiaoBut.frame = CGRectMake(20,self.webviewwangyeView.bottom+8, 70, 44);
               self.quxiaoBut.hidden = YES;
        self.shengjingBut.frame = CGRectMake(20,self.webviewwangyeView.bottom+8, kScreenW -80-20-20, 44);
        
        self.shengjingBut.backgroundColor =  [UIColor  bm_colorGradientChangeWithSizeView: self.shengjingBut];
        
        
        
    } else {
        
        
        NSLog(@"kjklkkll ");
        
        
        self.quxiaoBut.frame = CGRectMake(20,self.webviewwangyeView.bottom+8, 70, 44);
        self.quxiaoBut.hidden = NO;
         self.shengjingBut.frame = CGRectMake(20+70+10,self.webviewwangyeView.bottom+8, kScreenW -80-20-70-10-20, 44);
        
        self.shengjingBut.backgroundColor =  [UIColor  bm_colorGradientChangeWithSizeView: self.shengjingBut];
        
        
    }
    
    
    
    
    
    
}











- (void)relayoutTheChildControls {
//    // self.whiteView.backgroundColor = [UIColor redColor];
//    self.whiteView.frame = CGRectMake(0, self.whibackvieY, kScreenW, 120);
//    self.grayView.frame = CGRectMake(0, self.whibackvieY+120, kScreenW, kScreenH-self.whibackvieY-118+10);
//

    
}






// 添加灰色View
-(void)addgrayView {
    
    self.grayView = [[UIView alloc]init];
    self.grayView.frame  = CGRectMake(0,0, kScreenW, kScreenH);
    
    self.grayView.backgroundColor =  RGB(51, 51, 51);
    self.grayView.alpha = 0.6;
    [self addSubview:self.grayView];
    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
//    //  tap.delegate = self;
//    [self.grayView addGestureRecognizer:tap];
//
//    
    
    
}

- (void)tapView:(UITapGestureRecognizer *)touch {
    
    [touch.view.superview removeFromSuperview];
    //   [[NSNotificationCenter defaultCenter] postNotificationName:@"notifyName1" object:nil];
    
    
}


//- (void)setZifuchuanStr:(NSString *)zifuchuanStr {
//
//      _zifuchuanStr = zifuchuanStr;
//
//    self.miahsulabel.text = zifuchuanStr;
//
//
//
//
//
//}




@end
